import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class WordCount {

    public Parameter parameter;

    public WordCount(String longparas) throws IOException {
        this.parameter = Tools.putParasToParameter(longparas);
    }

    /**
     * @Auther: kevin
     * @param
     * @return: void
     * @Descriptions:控制整个流程，单个文件处理还是多文件的递归，或者调出ＵＩ。
     * @date: 18-10-17 下午8:47
     */
    public void processFile() throws IOException {

        String outPutFile = null;
        String inputData = null;
        String textData = null;
        Result result = null;
       if(parameter.isAdvance()){

           Client swingUI = new Client(parameter);
           swingUI.createAndShowGUI();

       }else if(parameter.isProcessMultFiles()){

            result = new Result();
            //System.out.println("size: "+parameter.getFilesName().size());
            for(int i=0;i<parameter.getFilesName().size();i++){

                textData = Tools.text2String(new File(this.parameter.getFilesName().get(i)));
                Tools.setResult(this,parameter,result,textData);
                outPutFile = result.getOutPutFileName();
                inputData = Tools.formatInputData(this.parameter.getFilesName().get(i),result.getCharCount(),result.getWordCount(),result.getLineCount(),result.getCodeLineCount(),result.getCommentLineCount(),result.getEmptyLineCount());
                Tools.writeFile(outPutFile,inputData);
                //System.out.println(outPutFile+inputData);
            }

        }else{
           result = new Result();
           textData = Tools.text2String(new File(this.parameter.getInputfileName()));
           Tools.setResult(this,parameter,result,textData);
           outPutFile = parameter.getOutPutFileName();
           inputData = Tools.formatInputData(parameter.getInputfileName(),result.getCharCount(),result.getWordCount(),result.getLineCount(),result.getCodeLineCount(),result.getCommentLineCount(),result.getEmptyLineCount());
           Tools.writeFile(outPutFile,inputData);
           //System.out.println(outPutFile+inputData);
       }

    }

    /**
     * @Auther: kevin
     * @param textData
     * @return: int
     * @Descriptions:获取字符数
     * @date: 18-10-17 下午8:47
     */
    public int getCharCount(String textData){
        int ret = -1;
        ret = textData.length();
        return ret;
    }

    /**
     * @Auther: kevin
     * @param HasStopWords
    * @param textData
     * @return: int
     * @Descriptions:获取词数
     * @date: 18-10-17 下午8:46
     */
    public int getWordCount(boolean HasStopWords,String textData){

        int ret = -1;
        String tmp[] = textData.split("\\s+|,+");
        List<String> list=new ArrayList<String>();

        for(int i=0;i<tmp.length;i++){
            if(!tmp[i].equals(""))
                list.add(tmp[i]);
        }

        //对停用词不进行计数
        if(HasStopWords){
            //File file = new File("/home/kevin/s.txt");
            File file = new File(parameter.getFilterFilename());
            String stopWord = Tools.text2String(file);
            String stopWords[] = stopWord.split("\\s+");
            for(int i=0;i<stopWords.length;i++){
                for(int j=0;j<list.size();j++){
                    if(stopWords[i].equalsIgnoreCase(list.get(j))){
                        //System.out.println(list.get(j));
                        list.remove(j);
                        j--;
                    }
                }
            }
        }

        ret = list.size();

        return ret;
    }

    /**
     * @Auther: kevin
     * @param textData
     * @return: int
     * @Descriptions:获取行数
     * @date: 18-10-17 下午8:46
     */
    public int getLineCount(String textData){

        int ret = -1;
        ret = textData.split(System.lineSeparator()).length;
        return ret;
    }

    /**
     * @Auther: kevin
     * @param textData
     * @return: int
     * @Descriptions:获取空行数
     * @date: 18-10-17 下午8:46
     */
    public int getEmptyLineCount(String textData){

        int ret = 0;
        String tmp[] = textData.split(System.lineSeparator());
        for(int i=0;i<tmp.length;i++){
            if(tmp[i].replaceAll("[\\s\\{\\}]","").equals(""))
                ret++;
        }
        return ret;
    }

    /**
     * @Auther: kevin
     * @param textData
     * @return: int
     * @Descriptions:获取代码行数
     * @date: 18-10-17 下午8:46
     */

    public int getCodeLineCount(String textData){
        int ret = 0;
        String tmp[] = textData.split(System.lineSeparator());
        ret = getLineCount(textData)-getCommentLineCount(textData)-getEmptyLineCount(textData);
        return ret;

    }

    /**
     * @Auther: kevin
     * @param textData
     * @return: int
     * @Descriptions:获取注释行数
     * @date: 18-10-17 下午8:49
     */
    public int getCommentLineCount(String textData){

        int ret = 0;
        String tmp[] = textData.split(System.lineSeparator());
        for(int i=0;i<tmp.length;i++){
            if(tmp[i].contains("//"))
                ret++;
        }
        return ret;
    }
}
